package com.atung.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.atung.dao.BusCompanionDAO;
import com.atung.dao.SchoolUserDAO;
import com.atung.model.BusCompanion;
import com.atung.model.BusSchoolUser;
import com.atung.request.RequestCompanion;
import com.atung.request.RequestPersonalMessage;
import com.atung.response.ResponseCompanion;
import com.atung.response.ResponsePersonalMessage;
import com.atung.service.PersonalCenterService;
import com.atung.tool.SnowflakeIdWorker;

import cn.hutool.core.bean.BeanUtil;

@Service
public class PersonalCenterServiceImpl implements PersonalCenterService {

	@Autowired
	private SchoolUserDAO schoolUserDao;
	@Autowired
	private BusCompanionDAO busCompanionDAO;
	@Override
	public ResponsePersonalMessage selectPersonalMsgAndOpenId(String openId)throws Exception {
		// TODO 自动生成的方法存根
		ResponsePersonalMessage responsePersonalMessage=new ResponsePersonalMessage();
		BusSchoolUser busSchoolUser=schoolUserDao.selectUserAndOpenId(openId);
		if(Objects.isNull(busSchoolUser)){
			//用户不存在
			throw new Exception("USER IS NULL");
		}
		BeanUtil.copyProperties(busSchoolUser, responsePersonalMessage);
		return responsePersonalMessage;
	}
	@Override
	public List<ResponseCompanion> selectCompanionAndOpenId(String openId) {
		// TODO 自动生成的方法存根
		List<ResponseCompanion> requestCompanionList=new ArrayList<ResponseCompanion>();
		List<BusCompanion> busCompanionList=busCompanionDAO.selectCompanionAndOpenId(openId);
		for(BusCompanion busCompanion:busCompanionList){
			ResponseCompanion responseCompanion=new ResponseCompanion();
			BeanUtil.copyProperties(busCompanion, responseCompanion);
			if(Objects.nonNull(responseCompanion.getCompanionName())&&!Objects.equals(responseCompanion.getCompanionName(),"")){
				responseCompanion.setSurname(String.valueOf(responseCompanion.getCompanionName().charAt(0)));
			}else{
				responseCompanion.setSurname(String.valueOf(responseCompanion.getCompanionName()));
			}
			requestCompanionList.add(responseCompanion);
		}
		return requestCompanionList;
	}
	
	@Override
	public Boolean updataMessageAndOpenId(RequestPersonalMessage requets) {
		// TODO 自动生成的方法存根
		BusSchoolUser busSchoolUser=new BusSchoolUser();
		BeanUtil.copyProperties(requets, busSchoolUser);
		busSchoolUser.setModificationTime(LocalDateTime.now());
		System.out.println(busSchoolUser);
		if(!Objects.equals(busSchoolUser.getUserSex(), 0)){
			System.out.println("------");
		}
		return schoolUserDao.updatetUserAndOpenId(busSchoolUser);
	}

	@Override
	public Boolean insertCompanion(RequestCompanion request) throws Exception {
		// TODO 自动生成的方法存根
		BusSchoolUser busSchoolUser=schoolUserDao.selectUserAndOpenId(request.getOpenId());
		if(Objects.isNull(busSchoolUser)){
			//用户不存在
			throw new Exception("USER IS NULL");
		}
		BusCompanion busCompanion=new BusCompanion();
		BeanUtil.copyProperties(request, busCompanion);
		busCompanion.setCreationTime(LocalDateTime.now());
		busCompanion.setModificationTime(LocalDateTime.now());
		busCompanion.setId(SnowflakeIdWorker.nextId());
		return busCompanionDAO.insertCompanion(busCompanion);
	}
	@Override
	public Boolean updateCompanion(RequestCompanion request) {
		// TODO 自动生成的方法存根
		BusCompanion busCompanion=new BusCompanion();
		BeanUtil.copyProperties(request, busCompanion);
		busCompanion.setModificationTime(LocalDateTime.now());
		return busCompanionDAO.updateCompanion(busCompanion);
	}
	@Override
	public Boolean delectCompanionAndId(Long id) {
		// TODO 自动生成的方法存根
		return busCompanionDAO.deleteCompanion(id);
	}

}
